const app = getApp()
const db = wx.cloud.database().collection("users")
var useropenid=''
var userid=''
var util=require('../../utils/util'); 
var jf=0
var userimage
Page({
  data: {
    motto: '北京科技大学气候碳索社会调查实践团',
    showAuthButton:true,
    Integral:0,
    frequency:0,
    user:{},
    listData:[]
  },
  //积分介绍
  jifenjieshao:function(){
    wx.navigateTo({
      url: '/pages/introduction/introduction',
    })
  },
  //获取openid
  getopid:function(){
    wx.cloud.callFunction({
      name:"getuseropenid",
      success(res){
        useropenid=res.result.openid
        app.globalData.openid=useropenid
      }
    })
  },
  //获取用户名
  getid:function(){
    wx.getUserInfo({
      success: function(res) {
        userid=res.userInfo.nickName
      }
    })
  },
  //查询数据
  getdata:function(){
    var that = this
    this.getid()
    db.where({
      _openid:useropenid
    }).get({
      success(res){
        if (res.data.length){
          var timenow=util.formatTime(new Date)
          if(timenow!=res.data[0].beforetime){
            var s=res.data[0].mark
            var _id=res.data[0]._id
            s++
            db.doc(_id).update({
              data:{
                mark:s,
                beforetime:timenow
              },
            })
            wx.showToast({
              title: '签到成功',
              icon: 'success',
              duration: 2000
            })
            const innerAudioContext = wx.createInnerAudioContext();//新建一个createInnerAudioContext();
            innerAudioContext.autoplay = true;//音频自动播放设置
            innerAudioContext.src = '/audio/success.m4a';//链接到音频的地址
            innerAudioContext.onPlay(() => {});//播放音效
          }
          db.get({
            success(res){
              app.globalData.frequency=res.data[0].frequency
              that.setData({
                Integral:res.data[0].mark
              })
              jf=res.data[0].mark
            }
          })
        }
        else {
          that.setdata();
        }
      },
    })
  },
  //创建数据
  setdata:function(){
    db.add({
      data:{
        name:userid,
        mark:0,
        frequency:10,
        beforetime:util.formatTime(new Date),
        sharetime:['2020-1-1'],
        image:userimage,
        tansuo:0,
        tansuotime:['2020-1-1']
      }
    })
  },
  //窗口跳转
  jumppage:function(){
    wx.navigateTo({
      url: '/pages/questions/questions',
    })
  },
  //获取授权
  getUserInfo:function(e){
    if(e.detail.errMsg!="getUserInfo:fail auth deny") //授权成功
    {
      this.setData({
        user:e.detail.userInfo,
        showAuthButton:false
      })
      userimage=e.detail.userInfo.avatarUrl
      this.getopid()
      this.getdata()
    }
  },
  onLoad:function(){
    this.getuserdata()
    var that=this;
     //是否已经授权
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          // 必须是在用户已经授权的情况下调用
            wx.getUserInfo({
            success: function(res) {
              that.setData({
                user:res.userInfo,
                showAuthButton:false,
              })
              userimage=res.userInfo.avatarUrl
              that.getopid()
              that.getdata()
            }
          })
        }
      }
    })
  },
  onShow:function(){
    var that=this
    db.where({
      _openid:app.globalData.openid
    }).get({
      success(res){
        that.setData({
          Integral:res.data[0].mark
        })
      },
    })
  },
  onShareAppMessage:function(){
    var that=this
    db.where({
      _openid:app.globalData.openid
    }).get({
      success(res){
        var timeof=util.formatTime(new Date)
        if(res.data[0].sharetime.indexOf(timeof)===-1){
          var frequency=res.data[0].frequency
          var sharetime=res.data[0].sharetime
          var _id=res.data[0]._id
          sharetime.push(timeof)
          frequency++
          db.doc(_id).update({
            data:{
              frequency:frequency,
              sharetime:sharetime
            },
          })
          app.globalData.frequency=frequency
        }
      }
    })
  },
  onShareTimeline:function(){
    var that=this
    db.where({
      _openid:app.globalData.openid
    }).get({
      success(res){
        var timeof=util.formatTime(new Date)
        if(res.data[0].sharetime.indexOf(timeof)===-1){
          var frequency=res.data[0].frequency
          var sharetime=res.data[0].sharetime
          var _id=res.data[0]._id
          sharetime.push(timeof)
          frequency++
          db.doc(_id).update({
            data:{
              frequency:frequency,
              sharetime:sharetime
            },
          })
          app.globalData.frequency=frequency
        }
      }
    })
  },
  getuserdata:function(){//排行榜
    var that=this
    wx.cloud.callFunction({
      name:"getuserslist",
      success(res){
        var data = res.result.data
        var length = data.length
        for(var i=0;i<=length-1;i++)
        {
          for(var j=i+1;j<=length-1;j++)
          {
            var t;
            if(data[i].mark < data[j].mark)
            {
              t=data[i]
              data[i]=data[j]
              data[j]=t
            }
          }
        }
        var data2=[]
        for(var i=0;i<=19;i++)
        {
          if(i===0) data[i].color="#8B008B"
          else if(i===1) data[i].color="#FF00FF"
          else if(i===2) data[i].color="#EE82EE"
          else data[i].color="#FFB6C1"
          data2.push(data[i])
        }
        that.setData({
          length:length,
          listData:data2,

        })
      }
    })
  },
})